﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Mvc;
using yao.website.Models;
using Titan;
using yao.Entity;
using yao.Common;

namespace yao.website.Controllers
{
    /// <summary>
    /// 共公控制器
    /// </summary>
    public class CommonController : BaseController
    {
        private int pageSize = 10;

        /// <summary>
        /// 客户等级
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CustomerLevel()
        {
            CustomerLevels customerLevels = new CustomerLevels();
            using (IDbSession cn = OpenSession())
            {
                QueryExpression query = new QueryExpression();
                query.EntityType = typeof(CustomerLevel);
                query.Selects.Add(CustomerLevel_.CustomerLevelName);
                query.Selects.Add(CustomerLevel_.CustomerLevelId);
                query.Wheres.Add(CustomerLevel_.CompanyId.TEqual(CurrentEmployee.CompanyId));
                cn.SelectCollection(customerLevels.Items, query);
            }
            return Json(customerLevels.Items);
        }

        /// <summary>
        /// 客户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Customer()
        {
            Customers customers = new Customers();
            using (IDbSession cn = OpenSession())
            {
                QueryExpression query = new QueryExpression();
                query.EntityType = typeof(Customer);
                query.Selects.Add(Customer_.CustomerName);
                query.Selects.Add(Customer_.CustomerId);
                query.Wheres.Add(Customer_.CompanyId.TEqual(CurrentEmployee.CompanyId));
                //query.PageSize = pageSize;
                //query.PageIndex = 1;
                cn.SelectCollection(customers.Items, query);
            }
            return Json(customers.Items);
        }

        /// <summary>
        /// 供应商
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Provider()
        {
            Providers providers = new Providers();
            using (IDbSession cn = OpenSession())
            {
                QueryExpression query = new QueryExpression();
                query.EntityType = typeof(Provider);
                query.Selects.Add(Provider_.CompanyName);
                query.Selects.Add(Provider_.ProviderId);
                query.Wheres.Add(Provider_.CompanyId.TEqual(CurrentEmployee.CompanyId));
                //query.PageSize = pageSize;
                //query.PageIndex = 1;
                cn.SelectCollection(providers.Items, query);
            }
            return Json(providers.Items);
        }

        /// <summary>
        /// 仓库
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Store()
        {
            Stores stores = new Stores();
            using (IDbSession cn = OpenSession())
            {
                QueryExpression query = new QueryExpression();
                query.EntityType = typeof(Store);
                query.Selects.Add(Store_.StoreName);
                query.Selects.Add(Store_.StoreId);
                query.Wheres.Add(Store_.CompanyId.TEqual(CurrentEmployee.CompanyId));
                cn.SelectCollection(stores.Items, query);
            }
            return Json(stores.Items);
        }

        /// <summary>
        /// 员工
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Employee()
        {
            Employees employees = new Employees();
            using (IDbSession cn = OpenSession())
            {
                QueryExpression query = new QueryExpression();
                query.EntityType = typeof(Employee);
                query.Selects.Add(Employee_.EmployeeName);
                query.Selects.Add(Employee_.EmployeeId);
                query.Wheres.Add(Employee_.CompanyId.TEqual(CurrentEmployee.CompanyId));
                cn.SelectCollection(employees.Items, query);
            }
            return Json(employees.Items);
        }

        /// <summary>
        /// 商品类别
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Category()
        {
            Categorys categorys = new Categorys();
            using (IDbSession cn = OpenSession())
            {
                QueryExpression query = new QueryExpression();
                query.EntityType = typeof(Category);
                query.Selects.Add(Category_.CategoryName);
                query.Selects.Add(Category_.CategoryId);
                cn.SelectCollection(categorys.Items, query);
            }
            return Json(categorys.Items);
        }

        /// <summary>
        /// 品牌
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Brand()
        {
            Brands brands = new Brands();
            using (IDbSession cn = OpenSession())
            {
                QueryExpression query = new QueryExpression();
                query.EntityType = typeof(Brand);
                query.Selects.Add(Brand_.BrandName);
                query.Selects.Add(Brand_.BrandId);
                cn.SelectCollection(brands.Items, query);
            }
            return Json(brands.Items);
        }

        /// <summary>
        /// 支付方式
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult PaymentType()
        {
            PaymentTypes paymentTypes = new PaymentTypes();
            using (IDbSession cn = OpenSession())
            {
                QueryExpression query = new QueryExpression();
                query.EntityType = typeof(PaymentType);
                query.Selects.Add(PaymentType_.PaymentTypeId);
                query.Selects.Add(PaymentType_.PaymentTypeName);
                cn.SelectCollection(paymentTypes.Items, query);
            }
            return Json(paymentTypes.Items);
        }
        /// <summary>
        /// 商品图片上传
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public ActionResult UploadImages(string path)
        {
            string rsPath = string.Empty;
            if (Request.Files.Count > 0)
            {
                foreach (string key in Request.Files)
                {
                    var file = this.Request.Files[key];
                    if (file == null)
                    {
                        continue;
                    }
                    if (file.ContentLength == 0)
                    {
                        continue;
                    }
                    string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
                    string extension = Path.GetExtension(file.FileName);
                    string fileName = Guid.NewGuid() + extension;
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    string originalImagePath = Path.Combine(filePath, fileName);

                    string image60 = Guid.NewGuid() + extension;
                    string image200 = Guid.NewGuid() + extension;
                    string image400 = Guid.NewGuid() + extension;
                    string image60Path = Path.Combine(filePath, image60);
                    string image200Path = Path.Combine(filePath, image200);
                    string image400Path = Path.Combine(filePath, image400);
                    file.SaveAs(originalImagePath);
                    CommonFun.MakeThumbnail(originalImagePath, image60Path, 60, 60, "Cut", "jpg");
                    CommonFun.MakeThumbnail(originalImagePath, image200Path, 200, 200, "Cut", "jpg");
                    CommonFun.MakeThumbnail(originalImagePath, image400Path, 400, 400, "Cut", "jpg");
                    rsPath = "/" + path + image60 + ";/" + path + image200 + ";/" + path + image400;
                }
            }

            return Json(new { result = rsPath });
        }
    }
}
